/*
t_service
t_val
t_returnXml
root
spf_getHttpRequest
sn_xmlHttpRequest
*/
//------------------------------------------------------------------------------------------------
function WebService (){};

WebService.sn_xmlHttpRequest = null;

WebService.spf_getHttpRequest = function() {
	if (WebService.sn_xmlHttpRequest == null) {
		WebService.sn_xmlHttpRequest= new ActiveXObject("MSXML2.XMLHTTP.3.0");
	}
	return WebService.sn_xmlHttpRequest;
};

WebService.call = function (t_service,t_val){
	var t_xmlHttp = WebService.spf_getHttpRequest();
	t_xmlHttp.open("post", t_service , false);
	t_xmlHttp.setRequestHeader("Content-Type","text/xml;charset=UTF-8");
	t_xmlHttp.send( t_val );
	var t_returnXml = t_xmlHttp.responseXML;
	var root = t_returnXml.selectSingleNode("root");
	switch(root.getAttribute("code")) {
		case "0":break;
		case "1":break;
		case "2":
			throw new Error(100, root.selectSingleNode("desc").firstChild.nodeValue);
		default: return "";
	}
	return root;
}

//------------------------------------------------------------------------------------------------


function Argument(type,val,no){
	this.type=type;
	this.val=val;
	this.no=no
}
Argument.TYPE_XML = "x";
Argument.TYPE_JSON = "j";
Argument.TYPE_STRING = "s";
Argument.TYPE_NO = "n";

//------------------------------------------------------------------------------------------------
function Servic(id,name,mothed,returnType){
	this.id=id;
	this.name=name;
	this.mothed=mothed;
	this.argument=null;
	if(typeof returnType != "undefined")
		this.returnType = returnType;
	else 
		this.returnType = Servic.returnType.string;
}

Servic.returnType = {
	xml:"x",
	json:"j",
	string:"s",
	no:"n"
}

Servic.prototype.setArgument=function(type,val,no){
	this.argument = new Argument(type,val,no);
}

//------------------------------------------------------------------------------------------------
function ServerImpL(){
	this.servics=new Array();
	this.returnVals = new Array();
}
ServerImpL.prototype.put=function(val){
	this.servics.push(val)
}
ServerImpL.prototype.exec=function(){
	try{
		var leng = this.servics.length;
		if(leng<1) {
			alert("please add service!")
			return;
		}
		var buffer=new StringBuffer();
		buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><requests>");
		//debugger;
		for (var i = 0; i < leng; i++ ) {
			var s = this.servics[i];
			/*
			*Id,服务名称,方法名称,返回类型
			*/
			buffer.append("<request id=\"" ).append(s.id ).append("\" server=\"");
			buffer.append( s.name ).append( "\" mothed=\"" ).append( s.mothed );
			buffer.append( "\" returntype=\"" ).append( s.returnType ).append( "\">");
			/*
			*方法的参数xml
			*/
			var argument  = s.argument;
			buffer.append("<arg type=\"");
			/*
			 分:有参数(xml:x,string:s,json:j),没有参数(null:n)
			*/
			if(argument !=null) {
				//xml
				if(argument.type === Argument.TYPE_XML){
					buffer.append(argument.type+"\">")
					buffer.append(argument.value);
				} else {//string或 json
					buffer.append(argument.type+"\"><![CDATA[")
					buffer.append(argument.value="]]>");
				}
			} else {
				buffer.append(Argument.TYPE_NO);
				buffer.append("\">");
			}
			buffer.append("</arg>");			
			buffer.append("</request>");
		}
		buffer.append("</requests></root>");	
		
		var root = WebService.call("test.service",buffer.toString())
		var results = root.selectSingleNode("results").childNodes;
		for ( var i = 0; i < results.length; i++) {
			var result = results.item(i);
			var returnVal =new  returnValue();
			returnVal.id = result.getAttribute("id");
			returnVal.type = result.getAttribute("type");
			switch(returnVal.type) {
				case "xml":
					returnVal.value=result.firstChild;
					break;
				case "json":
					returnVal.value=eval(result.firstChild.nodeValue);
					break;				
				default: 
					returnVal.value=result.firstChild.nodeValue;
					break;
			}
			this.returnVals.push(returnVal);
		}
	}catch(e) {
		throw new Error(100, e.description);
	}
}

ServerImpL.prototype.getReturnValue=function(id){
	var leng = this.returnVals.length
	if(leng < 1) {
		return null;
	}
	if(typeof id == "undefined") {
		return this.returnVals[0];
	}
	
	for ( var i = 0; i < leng; i++ ) {
		if(this.returnVals[i]==id) 
			return this.returnVals[i];
	}
	return null;
}
